﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.IO;
using ChinaClipping.ClippingMaster.MyTools;
namespace ChinaClipping.ClippingMaster
{
    public partial class Dlg_Config : Form
    {
        Wrapper _wrapper = new Wrapper(MySettings.KEY);
        public Dlg_Config()
        {
            InitializeComponent();
        }
        public Dlg_Config(int tabpageindex):this()
        {
          if(tabpageindex <tabControl1 .TabPages .Count )
            tabControl1.SelectedIndex = tabpageindex;
        }
        private void Dlg_Config_Load(object sender, EventArgs e)
        {
            Tbx_UserName.Text = Properties.Settings.Default.UserName;

            //Tbx_FtpAddress.Text = Properties.Settings.Default.FtpAddress;
            //if(!string.IsNullOrEmpty (Properties.Settings.Default.ftpuser))
            //Tbx_FtpUser.Text = _wrapper.DecryptString(Properties.Settings.Default.ftpuser);
            //if (!string.IsNullOrEmpty(Properties.Settings.Default.ftppwd))
            //Tbx_FtpPassword.Text = _wrapper.DecryptString(Properties.Settings.Default.ftppwd);

            Tbx_HttpAddress.Text = Properties.Settings.Default.HttpAddress;
            if (!string.IsNullOrEmpty(Properties.Settings.Default.HttpUser))
                Tbx_HttpUser.Text = _wrapper.DecryptString(Properties.Settings.Default.HttpUser);
            if (!string.IsNullOrEmpty(Properties.Settings.Default.HttpPwd))
                Tbx_HttpPwd.Text = _wrapper.DecryptString(Properties.Settings.Default.HttpPwd);



            Chbx_AutoGetContent.Checked = Properties.Settings.Default.AutoGetContent;
            Chbx_AutoOpenUrl.Checked = Properties.Settings.Default.AutoOpenNews;
            checkBox_AutoUpdate.Checked = Properties.Settings.Default.AutoUpdate;

        }

        private void OK_Button_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.UserName = Tbx_UserName.Text;


            //Properties.Settings.Default.FtpAddress = Tbx_FtpAddress.Text;
            //if(!string.IsNullOrEmpty (Tbx_FtpUser.Text))
            //Properties.Settings.Default.ftpuser = _wrapper.EncryptString (Tbx_FtpUser.Text);
            //if(!string.IsNullOrEmpty (Tbx_FtpPassword .Text ))
            //Properties.Settings.Default.ftppwd = _wrapper.EncryptString(Tbx_FtpPassword.Text);


            Properties.Settings.Default.HttpAddress = Tbx_HttpAddress.Text;
            if (!string.IsNullOrEmpty(Tbx_HttpUser.Text))
                Properties.Settings.Default.HttpUser = _wrapper.EncryptString(Tbx_HttpUser.Text);
            if (!string.IsNullOrEmpty(Tbx_HttpPwd.Text))
                Properties.Settings.Default.HttpPwd = _wrapper.EncryptString(Tbx_HttpPwd.Text);

            Properties.Settings.Default.AutoGetContent = Chbx_AutoGetContent.Checked;
            Properties.Settings.Default.AutoOpenNews = Chbx_AutoOpenUrl.Checked;
            Properties.Settings.Default.AutoUpdate = checkBox_AutoUpdate.Checked;
            Properties.Settings.Default.Save();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Btn_UpdateClient_Click(object sender, EventArgs e)
        {
            if (Uri.IsWellFormedUriString(Properties.Settings.Default.ClientInfoUrl, UriKind.Absolute))
            {
                WebClient wc = new WebClient();
                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                wc.Encoding = Encoding.UTF8;
                Uri clienturl = new Uri(Properties.Settings.Default.ClientInfoUrl);
                wc.DownloadStringAsync(clienturl);
            }
            else
            {
                MessageBox.Show("尚未设置用于更新信息的地址，无法执行更新操作！","无法更新",MessageBoxButtons .OK ,MessageBoxIcon.Error);
            }
        }

        void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            PB_UpdateClient.Value = e.ProgressPercentage;
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string xmlstr = e.Result;
                XmlDataDocument xml = new XmlDataDocument();
                xml.LoadXml(xmlstr);
                //xml.Save(MySettings.ClientInfoFile);
                Wrapper w=new Wrapper (MySettings.KEY );
                string enstr = w.EncryptString(xmlstr);
                File.WriteAllText(MySettings.ClientInfoFile, enstr,Encoding.UTF8);
                MessageBox.Show("更新客户分类信息成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("更新客户分类信息失败：{0}{1}", Environment.NewLine, ex.Message), "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                PB_UpdateClient.Value = 0;
            }
            
            
        }

        private void Btn_UpdateUser_Click(object sender, EventArgs e)
        {

            if (Uri.IsWellFormedUriString(Properties.Settings.Default.UserInfoUrl, UriKind.Absolute))
            {
                WebClient wc = new WebClient();
                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged1);
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted1);
                wc.Encoding = Encoding.UTF8;
                Uri userurl = new Uri(Properties.Settings.Default.UserInfoUrl);
                wc.DownloadStringAsync(userurl);

            }
            else
            {
                MessageBox.Show("尚未设置用于更新信息的地址，无法执行更新操作！", "无法更新", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void wc_DownloadStringCompleted1(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string xmlstr = e.Result;
                XmlDataDocument xml = new XmlDataDocument();
                xml.LoadXml(xmlstr);
                //xml.Save(MySettings.ClientInfoFile);
                Wrapper w = new Wrapper(MySettings.KEY);
                string enstr = w.EncryptString(xmlstr);
                File.WriteAllText(MySettings.UserListFile, enstr, Encoding.UTF8);
                MessageBox.Show("更新用户信息成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("更新用户信息失败：{0}{1}", Environment.NewLine, ex.Message), "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                PB_UpdateUser.Value = 0;
            }
        }

      
        void wc_DownloadProgressChanged1(object sender, DownloadProgressChangedEventArgs e)
        {
          PB_UpdateUser.Value = e.ProgressPercentage;
        }

    }
}
